//
//  DLHeaderSelectTabView.m
//  DGGUISpaceSDK
//
//  Created by 伍明鹏 on 2019/12/20.
//

#import "DLHeaderSelectTabView.h"
#import "DLHeaderSelectTabModel.h"
#import "DLEdgeInsetsButton.h"
#define kSetValue(key,value) key ? key : value

@interface DLHeaderSelectTabView()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView   *tabView;
@end
@implementation DLHeaderSelectTabView

- (instancetype)init{
    if (self = [super init]) {
        [self setupUIWithSelectTabArray:self.selectTabArray];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)]) {
        [self setupUIWithSelectTabArray:self.selectTabArray];
    }
    return self;
}

#pragma mark  - setUI
- (void)setupUIWithSelectTabArray:(NSArray <DLHeaderSelectTabModel*>*)selectTabArray;
{
    self.backgroundColor = [[DLThemeAdapter shareInstance] dynamicColor:[UIColor whiteColor] darkColor:[UIColor colorWithHex:0x2D2D2E]];
    NSMutableArray *buttonArray = @[].mutableCopy;
    if (selectTabArray.count < 1) {
        return;
    }
    
    if (self.selectTabArray.count > 4) {
        self.tabView.contentSize = CGSizeMake(self.selectTabArray.count*SCREEN_WIDTH/4, 0);
    }
    //标签按钮
    UIView *preView = self.tabView;
    for (int i = 0; i < selectTabArray.count; i++) {
        DLEdgeInsetsButton *selectButton = [DLEdgeInsetsButton buttonWithType:UIButtonTypeCustom];
        [buttonArray addObject:selectButton];
        selectButton.tag = 10+i;
        selectButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        selectButton.backgroundColor = [[DLThemeAdapter shareInstance] dynamicColor:[UIColor whiteColor] darkColor:[UIColor colorWithHex:0x2D2D2E]];
        [selectButton setTitle:selectTabArray[i].name  forState:UIControlStateNormal];
        [selectButton setImage:kSetValue(self.normalImage, IMAGE_NAME(@"img_tab_n_jiantou")) forState:UIControlStateNormal];
        [selectButton setImage:kSetValue(self.selectImage, IMAGE_NAME(@"img_tab_s_jiantou")) forState:UIControlStateSelected];
//        [selectButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 30)];
//        [selectButton setImageEdgeInsets:UIEdgeInsetsMake(0, 90, 0, 0)];
        [selectButton setTitleColor:kSetValue(self.normalColor, [[DLThemeAdapter shareInstance] dynamicColor:[UIColor colorWithHex:0x333333] darkColor:[UIColor colorWithHex:0xE0E0E0]])  forState:UIControlStateNormal];
        [selectButton setTitleColor:kSetValue(self.selectColor, [UIColor colorWithHex:0x1CBEBC])  forState:UIControlStateSelected];
        selectButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.tabView addSubview:selectButton];
        
        NSInteger count = selectTabArray.count;
        [selectButton mas_makeConstraints:^(MASConstraintMaker *make) {
            if (count < 5) {
                make.left.equalTo(self.tabView).offset(i*SCREEN_WIDTH/count);
                make.width.mas_equalTo(SCREEN_WIDTH/count);
            }else{
                 make.left.equalTo([preView isKindOfClass:[UIButton class]] ? preView.mas_right : preView.mas_left);
                make.width.mas_equalTo(SCREEN_WIDTH/4);
            }
            make.top.equalTo(self.tabView).offset(0.5);
            make.height.mas_equalTo(43);
        }];
        
        @weakify(self);
        [[selectButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            selectButton.selected = YES;
            for (UIButton *button in buttonArray) {
                if (button.tag != 10+i) {
                    button.selected = NO;
                }
            }
            @strongify(self);
            if (self.selectClick) {
                self.selectClick(selectTabArray[i]);
            }
        }];
        if (i == 0) {
            selectButton.selected = YES;
            if (self.selectClick) {
                self.selectClick(selectTabArray[0]);
            }
        }
        selectButton.edgeInsetsStyle = DLButtonEdgeInsetsStyleImageRight;
        selectButton.imageTitleSpace = 10;
        preView = selectButton;
    }
    
    //分割线
    UIView *navTopLine = [UIView new];
    navTopLine.backgroundColor = kSetValue(self.lineColor, [UIColor colorWithHex:0xE5E5E5]);
    [self addSubview:navTopLine];
    UIView *navBottomLine = [UIView new];
    navBottomLine.backgroundColor = kSetValue(self.lineColor, [UIColor colorWithHex:0xE5E5E5]);
    [self addSubview:navBottomLine];
    
    [navTopLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
        make.height.mas_equalTo(0.5);
    }];
    [navBottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.bottom.equalTo(self.mas_bottom);
        make.height.mas_equalTo(0.5);
    }];
    
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
}


#pragma mark - lazy

- (void)setSelectTabArray:(NSArray<DLHeaderSelectTabModel *> *)selectTabArray{
    _selectTabArray = selectTabArray;
    [self setupUIWithSelectTabArray:_selectTabArray];
}

- (UIScrollView *)tabView {
    if (!_tabView) {
        self.tabView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0,self.width,44)];
        _tabView.delegate = self;
        _tabView.userInteractionEnabled = YES;
        _tabView.showsHorizontalScrollIndicator = NO;
        _tabView.scrollEnabled = YES;
        [self addSubview:_tabView];
    }
    return _tabView;
}
@end
